package com.yuyou.fn.educational.service.impl;

import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.ITeacherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yuyou.fn.educational.mapper.ClassTeacherMapper;
import com.yuyou.fn.educational.entity.ClassTeacher;
import com.yuyou.fn.educational.service.IClassTeacherService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * ClassTeacher 表数据服务层接口实现类
 *
 */
@Service
public class ClassTeacherServiceImpl extends SuperServiceImpl<ClassTeacherMapper, ClassTeacher,Long> implements IClassTeacherService {

    private static final Logger log  = LoggerFactory.getLogger(ClassTeacherServiceImpl.class);


    @Resource
    private ITeacherService teacherService ;

    @Override
    public void saveTeachers(Long classId, List<Long> teacherIds , Integer role ) {

        List<Teacher> teacherList = teacherService.findByIds(teacherIds);
        List<ClassTeacher> classTeacherList = new ArrayList<>(teacherIds.size());
        for(Teacher teacher : teacherList ){
            ClassTeacher classTeacher = new ClassTeacher();
            classTeacher.setClassId(classId);
            classTeacher.setRole(role);
            classTeacher.setTeacherId(teacher.getTeacherId());
            classTeacher.setTeacherName(teacher.getTeacherName());
            classTeacher.setCreateTime(TimeUtils.now());
            classTeacher.setUpdateTime(TimeUtils.now());
            classTeacherList.add(classTeacher);
        }

        try{
            insertBatch(classTeacherList);
        }catch (Exception e){
            log.warn("insertBatch fail ! \n{}",e);
            for(ClassTeacher classTeacher : classTeacherList ){
                try{
                    insertSelective(classTeacher);
                }catch (Exception e1 ){
                    log.warn("insertSelective fail ! \n{}",e);
                }

            }
        }



    }
}